﻿<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page import="my.myorm.tool.jdbc.SQLExecutor" %>
<%@ page import="my.myorm.tool.jdbc.SQLManager" %>
<%@ page import="java.util.Date" %>
<%@ page import="my.myorm.tool.jdbc.ResultList" %>
<%@ page import="com.util.SuperString" %>
<%@ page import="com.util.SuperPage" %>
<%@ page import="com.mtbu.comm.Util" %>
<%@ page import="com.mtbu.dao.DAOManager" %>
<%@ page import="com.mtbu.service.LogUtil" %>
<%@ page import="com.mtbu.bean.*"%>
<%@ page import="com.util.SuperDate"%>
<%@ page import="java.io.File"%>
<%@ page import="my.tool.ConfigProp" %>
<%@ page import="com.mtbu.comm.*"%>
<%@ page import="my.mypool.core.memcache.*"%>

<%@ include file="sys_commcheck.jsp" %>



<%
	//System.out.println("request:" + request.getRequestURI());
	//System.out.println("request:" + request.getRequest());

    String act = SuperString.notNullTrim(request.getParameter("act"));
	boolean isBatchdel = act.equalsIgnoreCase("batchdel");
	//System.out.println("isBatchdel:" + isBatchdel);
    if(!act.equalsIgnoreCase("mod") && !act.equalsIgnoreCase("del")) act = "add";
    boolean isAdd = act.equalsIgnoreCase("add");
    boolean isMod = act.equalsIgnoreCase("mod");
    boolean isDel = act.equalsIgnoreCase("del");



    String curFile = SuperPage.getCurURLFile(request);
    String backFile = SuperString.replace(curFile, "moddo", "");
    String curName = "CFG";

    int id = SuperString.getInt(request.getParameter("id"));
	String key = SuperString.notNullTrim(request.getParameter("key"));
	String value = SuperString.notNullTrim(request.getParameter("value"));

    if(!isBatchdel && !isAdd && id <= 0) {
        Util.callInfo("错误的调用方法！", request, response);
        return;
    }
    SuperDate sdnow = new SuperDate();
	CommonCfg record = null;

    if(isDel) {

		record = DAOManager.commonCfgDao.findCommonCfg(id);

		if(record == null){
			Util.callInfo("删除失败，没有找到该id的记录！",request, response);
			return;
		}

		try{
			DAOManager.commonCfgDao.delete(record);
		}
		catch(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e){
			Util.callInfo("删除失败，破坏参照完整性！", request, response);
			return;
		}
    }

	if(isAdd) {

		record = new CommonCfg();

		try{
			record.setCfgKey(key);
			record.setValue(value);

			record.setUpdateTime(sdnow.getDate());


			DAOManager.commonCfgDao.save(record);
		}
		catch(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e){
			Util.callInfo("增加失败:" + e.getMessage(), request, response);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			Util.callInfo("增加失败:" + e.getMessage(), request, response);
			return;
		}
	}


	if(isMod) {

		record = DAOManager.commonCfgDao.findCommonCfg(id);

		if(record == null){
			Util.callInfo("修改失败，没有找到该id的记录！",request, response);
			return;
		}

		try{
			record.setCfgKey(key);
			record.setValue(value);
			record.setUpdateTime(sdnow.getDate());

			DAOManager.commonCfgDao.update(record);

		}
		catch(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e){
			e.printStackTrace();
			Util.callInfo("修改失败:" + e.getMessage(), request, response);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			Util.callInfo("修改失败:" + e.getMessage(), request, response);
			return;
		}
    }

/*
	String serverIp = PropUtil.getInstance("memcache").getProperty("my.memcache.server.host", "");
	int serverPort = Integer.parseInt( PropUtil.getInstance("memcache").getProperty("my.memcache.server.port") ,10);
	int idle = Integer.parseInt( PropUtil.getInstance("memcache").getProperty("my.memcache.server.maxIdle") ,10);
	int initIdleCapacity = Integer.parseInt( PropUtil.getInstance("memcache").getProperty("my.memcache.server.initIdleCapacity") ,10);
	int expire = Integer.parseInt( PropUtil.getInstance("memcache").getProperty("my.memcache.server.expire") ,10);

	MemCacheClient mcc = new MemCacheClient(serverIp, serverPort, idle, initIdleCapacity);

	System.out.println( "mcc.flush:" + mcc.flush() );
*/
	LogUtil.writeLog(Constant_UserName, Util.getActText(act) + curName,
			id + "|" + record.toLogString(), request, null);


    response.sendRedirect(backFile + SuperPage.getQueryQ(request, "act,id"));
    return;
%>